package com.wzl.jdbc;

import com.wzl.jdbc.entity.TUser;
import com.wzl.jdbc.service.TUserService;
import org.apache.shardingsphere.api.hint.HintManager;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weizhongli
 * @since 1.0, 2024/05/13 10:45
 */
@SpringBootTest
public class test {


    @Autowired
    private TUserService userService;

    @Test
    void saveBatchTest() {
        List<TUser> users = new ArrayList<>();
        users.add(new TUser("AAAAAA", "123456", 16,"表t_user_1"));
        users.add(new TUser("BBBBBB", "123456", 17,"表t_user_2"));
        users.add(new TUser("CCCCCC", "123456", 18,"表t_user_0"));
//        users.add(new TUser("DDD", "123456", 4,"表t_user_1"));
//        users.add(new TUser("EEE", "123456", 5,"表t_user_2"));
//        users.add(new TUser("FFF", "123456", 6,"表t_user_0"));

        userService.saveBatch(users);
    }

    @Test
    void listTest() {
        List<TUser> users = userService.list();
        System.out.println(">>>>>>>>>> 【Result】<<<<<<<<<< ");
        users.forEach(System.out::println);
    }


    /**
     * 精准分片算法策略
     * select  *  from  t_user  where  number  = ?  or  number  in (?,?)
     */
    @Test
    void getUserPreciseShardingAlgorithm() {
        TUser user = userService.getUserByNumber(16);
        System.out.println("【Result】>>>>>>>>> " + user);
    }

    /**
     * 范围分片算法策略
     * select  *  from  t_user  where number between 1 and 100
     */
    @Test
    void getUserRangeShardingAlgorithm() {
        List<TUser> user = userService.getUserBetween(1,5);
        System.out.println("【Result】>>>>>>>>> ");
        user.forEach(System.out::println);
    }

    /**
     * hint分片算法策略
     */
    @Test
    void queryCourseHint() {
        HintManager.clear();
        HintManager hintManager = HintManager.getInstance();
        // hintManager.addDatabaseShardingValue("m",1);
        hintManager.addTableShardingValue("t_user", 1);
        List<TUser> courses = userService.selectList(null);
        courses.forEach(user -> System.out.println(user));
        hintManager.close();
    }

}
